package com.mtwp.dfs.service.dfs;

import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.exception.FdfsUnsupportStorePathException;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.github.tobato.fastdfs.service.TrackerClient;
import com.mtwp.dfs.config.AppConfig;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;

@Service
@Transactional
@Slf4j
public class FastDFSService {
    @Autowired
    private FastFileStorageClient storageClient;

    @Autowired
    AppConfig appConfig;

    @Autowired
    TrackerClient trackerClient;

    /**
     * 上传文件
     * @param file 文件对象
     * @return 文件访问地址
     * @throws IOException
     */
    public String uploadFile(String userName, MultipartFile file) throws IOException {
        String fileExtName = appConfig.getName()+"/"+userName+"/"+file.getOriginalFilename();
        StorePath storePath = storageClient
                                    .uploadFile(
                                            file.getInputStream(),
                                            file.getSize(),
                                            fileExtName,
                                            null);
        return getResAccessUrl(storePath);
    }

    /**
     * 将一段字符串生成一个文件上传
     * @param content 文件内容
     * @param fileExtension
     * @return
     */
    public String uploadFile(String userName,String content, String fileExtension) {
        String fileExtName = userName+"/"+FilenameUtils.getExtension(fileExtension);
        byte[] buff = content.getBytes(Charset.forName("UTF-8"));
        ByteArrayInputStream stream = new ByteArrayInputStream(buff);
        StorePath storePath = storageClient.uploadFile(stream,buff.length, fileExtName,null);
        return getResAccessUrl(storePath);
    }

    // 封装图片完整URL地址
    private String getResAccessUrl(StorePath storePath) {
        String fileUrl = appConfig.getHttpProdocol() + appConfig.getResHost()
                + ":" + appConfig.getFdfsStoragePort() + "/" + storePath.getFullPath();
        return fileUrl;
    }

    /**
     * 删除文件
     * @param fileUrl 文件访问地址
     * @return
     */
    public void deleteFile(String fileUrl) throws FdfsUnsupportStorePathException{
        if (StringUtils.isEmpty(fileUrl)) {
            return;
        }
        StorePath storePath = StorePath.praseFromUrl(fileUrl);
        storageClient.deleteFile(storePath.getGroup(), storePath.getPath());
    }

    /**
     * 批量删除文件
     * @param fileUrlList 文件访问地址
     * @return
     */
    public void deleteInFileList(List<String> fileUrlList) {
        if (fileUrlList.isEmpty()) return;
        fileUrlList.forEach(
                f-> deleteFile(f)
        );
    }
}
